#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaCheckBox.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QDebug>

class ElaCheckBoxTest : public ElaWidget
{
    Q_OBJECT

  public:
    ElaCheckBoxTest(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaCheckBox 功能演示");
        resize(700, 600);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        setupUI();
        setupConnections();
    }

  private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 基础复选框展示
        mainLayout->addWidget(createBasicCheckBoxGroup());

        // 2. 圆角样式展示
        mainLayout->addWidget(createBorderRadiusGroup());

        // 3. 状态演示
        mainLayout->addWidget(createStateDemoGroup());

        // 4. 复选框组应用
        mainLayout->addWidget(createCheckBoxGroupDemo());

        // 5. 动态操作演示
        mainLayout->addWidget(createDynamicOperationGroup());

        mainLayout->addStretch();
    }

    // 创建基础复选框组
    QGroupBox *createBasicCheckBoxGroup()
    {
        QGroupBox *group = new QGroupBox("基础复选框", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示基本的复选框功能", this);
        layout->addWidget(infoLabel);

        // 单个复选框
        ElaCheckBox *check1 = new ElaCheckBox("默认未选中", this);
        ElaCheckBox *check2 = new ElaCheckBox("默认选中", this);
        check2->setChecked(true);
        ElaCheckBox *check3 = new ElaCheckBox("三态复选框", this);
        check3->setTristate(true);  // 启用三态支持

        layout->addWidget(check1);
        layout->addWidget(check2);
        layout->addWidget(check3);

        m_basicChecks << check1 << check2 << check3;

        return group;
    }

    // 创建圆角样式展示组
    QGroupBox *createBorderRadiusGroup()
    {
        QGroupBox *group = new QGroupBox("圆角样式展示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同圆角大小的复选框", this);
        layout->addWidget(infoLabel);

        // 不同圆角大小的复选框
        ElaCheckBox *sharpCheck = new ElaCheckBox("直角 (0px)", this);
        sharpCheck->setBorderRadius(0);

        ElaCheckBox *smallRoundCheck = new ElaCheckBox("小圆角 (2px)", this);
        smallRoundCheck->setBorderRadius(2);

        ElaCheckBox *defaultCheck = new ElaCheckBox("默认圆角 (3px)", this);
        defaultCheck->setBorderRadius(3);  // 默认值

        ElaCheckBox *mediumRoundCheck = new ElaCheckBox("中等圆角 (5px)", this);
        mediumRoundCheck->setBorderRadius(5);

        ElaCheckBox *largeRoundCheck = new ElaCheckBox("大圆角 (8px)", this);
        largeRoundCheck->setBorderRadius(8);

        layout->addWidget(sharpCheck);
        layout->addWidget(smallRoundCheck);
        layout->addWidget(defaultCheck);
        layout->addWidget(mediumRoundCheck);
        layout->addWidget(largeRoundCheck);

        m_radiusChecks << sharpCheck << smallRoundCheck << defaultCheck << mediumRoundCheck << largeRoundCheck;

        return group;
    }

    // 创建状态演示组
    QGroupBox *createStateDemoGroup()
    {
        QGroupBox *group = new QGroupBox("状态演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示复选框的不同状态", this);
        layout->addWidget(infoLabel);

        // 正常状态
        ElaCheckBox *normalCheck = new ElaCheckBox("正常状态（可选择）", this);
        normalCheck->setChecked(true);

        // 禁用状态 - 未选中
        ElaCheckBox *disabledUnchecked = new ElaCheckBox("禁用状态 - 未选中", this);
        disabledUnchecked->setEnabled(false);

        // 禁用状态 - 选中
        ElaCheckBox *disabledChecked = new ElaCheckBox("禁用状态 - 选中", this);
        disabledChecked->setChecked(true);
        disabledChecked->setEnabled(false);

        // 禁用状态 - 部分选中（三态）
        ElaCheckBox *disabledPartial = new ElaCheckBox("禁用状态 - 部分选中", this);
        disabledPartial->setTristate(true);
        disabledPartial->setCheckState(Qt::PartiallyChecked);
        disabledPartial->setEnabled(false);

        layout->addWidget(normalCheck);
        layout->addWidget(disabledUnchecked);
        layout->addWidget(disabledChecked);
        layout->addWidget(disabledPartial);

        m_stateChecks << normalCheck << disabledUnchecked << disabledChecked << disabledPartial;

        return group;
    }

    // 创建复选框组应用演示
    QGroupBox *createCheckBoxGroupDemo()
    {
        QGroupBox *group = new QGroupBox("复选框组应用", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示复选框在实际场景中的应用", this);
        layout->addWidget(infoLabel);

        // 设置选项组
        QGroupBox *settingsGroup = new QGroupBox("应用程序设置", this);
        QVBoxLayout *settingsLayout = new QVBoxLayout(settingsGroup);

        ElaCheckBox *autoSave = new ElaCheckBox("自动保存文档", this);
        autoSave->setChecked(true);
        ElaCheckBox *showLineNumbers = new ElaCheckBox("显示行号", this);
        ElaCheckBox *wordWrap = new ElaCheckBox("自动换行", this);
        wordWrap->setChecked(true);
        ElaCheckBox *showStatusBar = new ElaCheckBox("显示状态栏", this);
        showStatusBar->setChecked(true);

        settingsLayout->addWidget(autoSave);
        settingsLayout->addWidget(showLineNumbers);
        settingsLayout->addWidget(wordWrap);
        settingsLayout->addWidget(showStatusBar);

        // 文件类型筛选组
        QGroupBox *filterGroup = new QGroupBox("文件类型筛选", this);
        QVBoxLayout *filterLayout = new QVBoxLayout(filterGroup);

        ElaCheckBox *imageFiles = new ElaCheckBox("图片文件 (.jpg, .png, .gif)", this);
        imageFiles->setChecked(true);
        ElaCheckBox *documentFiles = new ElaCheckBox("文档文件 (.pdf, .doc, .txt)", this);
        ElaCheckBox *audioFiles = new ElaCheckBox("音频文件 (.mp3, .wav, .flac)", this);
        ElaCheckBox *videoFiles = new ElaCheckBox("视频文件 (.mp4, .avi, .mkv)", this);

        filterLayout->addWidget(imageFiles);
        filterLayout->addWidget(documentFiles);
        filterLayout->addWidget(audioFiles);
        filterLayout->addWidget(videoFiles);

        layout->addWidget(settingsGroup);
        layout->addWidget(filterGroup);

        m_applicationChecks << autoSave << showLineNumbers << wordWrap << showStatusBar;
        m_filterChecks << imageFiles << documentFiles << audioFiles << videoFiles;

        return group;
    }

    // 创建动态操作演示组
    QGroupBox *createDynamicOperationGroup()
    {
        QGroupBox *group = new QGroupBox("动态操作演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("演示复选框的动态操作", this);
        layout->addWidget(infoLabel);

        // 动态修改文本
        QHBoxLayout *dynamicTextLayout = new QHBoxLayout();
        m_dynamicTextCheck = new ElaCheckBox("原始文本", this);
        QPushButton *changeTextBtn = new QPushButton("修改文本", this);

        dynamicTextLayout->addWidget(m_dynamicTextCheck);
        dynamicTextLayout->addWidget(changeTextBtn);
        dynamicTextLayout->addStretch();

        // 动态启用/禁用
        QHBoxLayout *enableDisableLayout = new QHBoxLayout();
        m_toggleEnableCheck = new ElaCheckBox("这个复选框会被启用/禁用", this);
        m_toggleEnableCheck->setChecked(true);
        QPushButton *toggleEnableBtn = new QPushButton("切换启用状态", this);

        enableDisableLayout->addWidget(m_toggleEnableCheck);
        enableDisableLayout->addWidget(toggleEnableBtn);
        enableDisableLayout->addStretch();

        // 全选/全不选功能
        QHBoxLayout *selectAllLayout = new QHBoxLayout();
        QLabel *selectAllLabel = new QLabel("批量操作:", this);
        QPushButton *selectAllBtn = new QPushButton("全选", this);
        QPushButton *deselectAllBtn = new QPushButton("全不选", this);
        QPushButton *invertSelectionBtn = new QPushButton("反选", this);

        selectAllLayout->addWidget(selectAllLabel);
        selectAllLayout->addWidget(selectAllBtn);
        selectAllLayout->addWidget(deselectAllBtn);
        selectAllLayout->addWidget(invertSelectionBtn);
        selectAllLayout->addStretch();

        // 批量操作的目标复选框
        QHBoxLayout *batchOperationLayout = new QHBoxLayout();
        m_batchCheck1 = new ElaCheckBox("选项 A", this);
        m_batchCheck2 = new ElaCheckBox("选项 B", this);
        m_batchCheck3 = new ElaCheckBox("选项 C", this);
        m_batchCheck4 = new ElaCheckBox("选项 D", this);

        batchOperationLayout->addWidget(m_batchCheck1);
        batchOperationLayout->addWidget(m_batchCheck2);
        batchOperationLayout->addWidget(m_batchCheck3);
        batchOperationLayout->addWidget(m_batchCheck4);
        batchOperationLayout->addStretch();

        // 状态显示
        QHBoxLayout *statusLayout = new QHBoxLayout();
        m_statusDisplay = new QLabel("选中状态: 无", this);

        statusLayout->addWidget(m_statusDisplay);
        statusLayout->addStretch();

        layout->addLayout(dynamicTextLayout);
        layout->addLayout(enableDisableLayout);
        layout->addLayout(selectAllLayout);
        layout->addLayout(batchOperationLayout);
        layout->addLayout(statusLayout);

        m_dynamicChecks << m_dynamicTextCheck << m_toggleEnableCheck << m_batchCheck1 << m_batchCheck2 << m_batchCheck3 << m_batchCheck4;
        m_operationButtons << changeTextBtn << toggleEnableBtn << selectAllBtn << deselectAllBtn << invertSelectionBtn;

        return group;
    }

    void setupConnections()
    {
        // 动态文本修改
        connect(m_operationButtons[0], &QPushButton::clicked, this, [this]() {
            static int clickCount = 0;
            clickCount++;
            QString newText = QString("动态修改的文本 %1").arg(clickCount);
            m_dynamicTextCheck->setText(newText);
            qDebug() << "复选框文本已修改为:" << newText;
        });

        // 启用/禁用切换
        connect(m_operationButtons[1], &QPushButton::clicked, this, [this]() {
            bool currentState = m_toggleEnableCheck->isEnabled();
            m_toggleEnableCheck->setEnabled(!currentState);
            QString stateText = !currentState ? "已启用" : "已禁用";
            qDebug() << "复选框状态:" << stateText;
        });

        // 全选功能
        connect(m_operationButtons[2], &QPushButton::clicked, this, [this]() {
            m_batchCheck1->setChecked(true);
            m_batchCheck2->setChecked(true);
            m_batchCheck3->setChecked(true);
            m_batchCheck4->setChecked(true);
            updateStatusDisplay();
            qDebug() << "已全选所有选项";
        });

        // 全不选功能
        connect(m_operationButtons[3], &QPushButton::clicked, this, [this]() {
            m_batchCheck1->setChecked(false);
            m_batchCheck2->setChecked(false);
            m_batchCheck3->setChecked(false);
            m_batchCheck4->setChecked(false);
            updateStatusDisplay();
            qDebug() << "已取消全选";
        });

        // 反选功能
        connect(m_operationButtons[4], &QPushButton::clicked, this, [this]() {
            m_batchCheck1->setChecked(!m_batchCheck1->isChecked());
            m_batchCheck2->setChecked(!m_batchCheck2->isChecked());
            m_batchCheck3->setChecked(!m_batchCheck3->isChecked());
            m_batchCheck4->setChecked(!m_batchCheck4->isChecked());
            updateStatusDisplay();
            qDebug() << "已反选所有选项";
        });

        // 批量操作复选框状态变化
        connect(m_batchCheck1, &ElaCheckBox::stateChanged, this, &ElaCheckBoxTest::updateStatusDisplay);
        connect(m_batchCheck2, &ElaCheckBox::stateChanged, this, &ElaCheckBoxTest::updateStatusDisplay);
        connect(m_batchCheck3, &ElaCheckBox::stateChanged, this, &ElaCheckBoxTest::updateStatusDisplay);
        connect(m_batchCheck4, &ElaCheckBox::stateChanged, this, &ElaCheckBoxTest::updateStatusDisplay);

        // 基础复选框信号
        for (int i = 0; i < m_basicChecks.size(); ++i)
        {
            connect(m_basicChecks[i], &ElaCheckBox::stateChanged, this, [i, this](int state) {
                QString stateText;
                switch (state)
                {
                    case Qt::Unchecked:
                        stateText = "未选中";
                        break;
                    case Qt::PartiallyChecked:
                        stateText = "部分选中";
                        break;
                    case Qt::Checked:
                        stateText = "选中";
                        break;
                }
                qDebug() << "基础复选框" << i + 1 << "状态:" << stateText << "-" << m_basicChecks[i]->text();
            });
        }

        // 应用设置复选框信号
        for (ElaCheckBox *check : m_applicationChecks)
        {
            connect(check, &ElaCheckBox::toggled, this, [check](bool checked) { qDebug() << "应用设置变更:" << check->text() << "=" << (checked ? "启用" : "禁用"); });
        }

        // 文件筛选复选框信号
        for (ElaCheckBox *check : m_filterChecks)
        {
            connect(check, &ElaCheckBox::toggled, this, [check](bool checked) { qDebug() << "文件筛选变更:" << check->text() << "=" << (checked ? "包含" : "排除"); });
        }
    }

  private Q_SLOTS:
    void updateStatusDisplay()
    {
        int checkedCount = 0;
        if (m_batchCheck1->isChecked())
            checkedCount++;
        if (m_batchCheck2->isChecked())
            checkedCount++;
        if (m_batchCheck3->isChecked())
            checkedCount++;
        if (m_batchCheck4->isChecked())
            checkedCount++;

        QString statusText = QString("选中状态: %1/%2 个选项").arg(checkedCount).arg(4);
        m_statusDisplay->setText(statusText);
    }

  private:
    // 基础复选框
    QList<ElaCheckBox *> m_basicChecks;

    // 圆角演示
    QList<ElaCheckBox *> m_radiusChecks;

    // 状态演示
    QList<ElaCheckBox *> m_stateChecks;

    // 应用演示
    QList<ElaCheckBox *> m_applicationChecks;
    QList<ElaCheckBox *> m_filterChecks;

    // 动态操作
    QList<ElaCheckBox *> m_dynamicChecks;
    QList<QPushButton *> m_operationButtons;

    // 动态操作控件指针
    ElaCheckBox *m_dynamicTextCheck;
    ElaCheckBox *m_toggleEnableCheck;
    ElaCheckBox *m_batchCheck1;
    ElaCheckBox *m_batchCheck2;
    ElaCheckBox *m_batchCheck3;
    ElaCheckBox *m_batchCheck4;
    QLabel *m_statusDisplay;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaCheckBoxTest demo;
    demo.moveToCenter();
    demo.show();

    return app.exec();
}

#include "ElaCheckBoxTest.moc"